//
//  StoreCollectionCell.m
//  GoBigMarket
//
//  Created by 李真 on 16/8/19.
//  Copyright © 2016年 legend. All rights reserved.
//

#import "HomeCollectionCell.h"
#import "MyProductsTool.h"
#import "HomeSixProductModel.h"

@implementation HomeCollectionCell{
    NSInteger _num;
}

- (NSMutableArray *)datas{
    if (_datas == nil) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    //获取通知中心单例对象
    NSNotificationCenter * centerJia = [NSNotificationCenter defaultCenter];
    //添加当前类对象为一个观察者，name和object设置为nil，表示接收一切通知
    [centerJia addObserver:self selector:@selector(productsJiaChange:) name:@"productsJiaChange" object:nil];
    //获取通知中心单例对象
    NSNotificationCenter * centerJian = [NSNotificationCenter defaultCenter];
    //添加当前类对象为一个观察者，name和object设置为nil，表示接收一切通知
    [centerJian addObserver:self selector:@selector(productsJianChange:) name:@"productsJianChange" object:nil];
    
    //获取通知中心单例对象
    NSNotificationCenter * centerLayoutRefresh = [NSNotificationCenter defaultCenter];
    //添加当前类对象为一个观察者，name和object设置为nil，表示接收一切通知
    [centerLayoutRefresh addObserver:self selector:@selector(layoutSubviews) name:@"layoutRefresh" object:nil];
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    self.commodityNumLabel.hidden = NO;
    self.jianBtn.hidden = NO;
    
    self.commodityNumLabel.text = [NSString stringWithFormat:@"%d",[MyProductsTool queryByCondition:self.tag]];

    if ([MyProductsTool queryByCondition:self.tag] <= 0) {
        self.commodityNumLabel.hidden = YES;
        self.jianBtn.hidden = YES;
    }
    //中划线
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:self.commodityYuanPirce.text attributes:attribtDic];
    
    self.commodityYuanPirce.attributedText = attribtStr;
    [self.commodityYuanPirce setTextColor:[UIColor grayColor]];
    [self.commodityPirce setTextColor:XNColor(236, 152, 18, 1)];
    
}

//监听添加商品通知
- (void)productsJiaChange:(NSNotification *)sender{
    
    NSDictionary *dict = sender.userInfo;
    NSNumber *tag = [dict valueForKey:@"tag"];
    if (self.tag == [tag integerValue]) {
        self.commodityNumLabel.hidden = NO;
        self.jianBtn.hidden = NO;
        [self layoutSubviews];
    }
}

//监听减少商品通知
- (void)productsJianChange:(NSNotification *)sender{

    NSDictionary *dict = sender.userInfo;
    NSNumber *tag = [dict valueForKey:@"tag"];
    if (self.tag == [tag integerValue]) {
        [self layoutSubviews];
    }
}


//消除通知中心
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"productsJiaChange" object:self];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"productsJianChange" object:self];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"layoutRefresh" object:self];

}

@end
